package net.lab1024.sa.admin.module.business.bjd.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.*;
import net.lab1024.sa.admin.module.business.bjd.domain.form.DollAddForm;
import net.lab1024.sa.admin.module.business.bjd.domain.form.DollDeleteForm;
import net.lab1024.sa.admin.module.business.bjd.domain.form.DollQueryForm;
import net.lab1024.sa.admin.module.business.bjd.domain.form.DollUpdateForm;
import net.lab1024.sa.admin.module.business.bjd.domain.vo.DollVO;
import net.lab1024.sa.admin.module.business.bjd.mapper.DollMapper;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @author guolong.zhang
 * @date 2025/02/09 09:35
 **/
@Service
@Slf4j
public class DollService {

    @Autowired
    private DollMapper dollMapper;

    public PageResult<DollVO> queryPage(DollQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<DollVO> list = dollMapper.pageList(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public void add(DollAddForm addForm) {
        //获取拼接娃娃ID
        String dollNo = getDollNo(addForm.getOfficialName(),addForm.getType(),
                addForm.getSize(),addForm.getSkin());

        DollEntity doll = Db.lambdaQuery(DollEntity.class)
                .eq(DollEntity::getDollNo, dollNo)
                .last(" limit 1 ")
                .one();
        if (doll != null) {
            throw new BusinessException("娃娃ID已存在");
        }
        doll = new DollEntity();
        doll.setOfficialName(addForm.getOfficialName());
        doll.setDollNo(dollNo);
        doll.setSkin(addForm.getSkin());
        doll.setSize(addForm.getSize());
        doll.setType(addForm.getType());
        doll.setImgUrl(addForm.getImgUrl());

        Db.save(doll);
    }

    public void update(DollUpdateForm updateForm) {
        DollEntity doll = Db.getById(updateForm.getId(), DollEntity.class);
        if (doll == null){
            throw new BusinessException("娃娃不存在");
        }
        //获取变更的dollNo
        String newDollNo = getDollNo(updateForm.getOfficialName(), updateForm.getType(), updateForm.getSize(), updateForm.getSkin());

        //获取变更前的dollNo
        String dollNo = doll.getDollNo();

        if(newDollNo.equals(dollNo)){
            //证明都是一样的，就是改图片
            Db.lambdaUpdate(DollEntity.class)
                    .eq(DollEntity::getId, updateForm.getId())
                    .set(DollEntity::getImgUrl, updateForm.getImgUrl())
                    .update();
        }else {
            doll.setOfficialName(updateForm.getOfficialName());
            doll.setSkin(updateForm.getSkin());
            doll.setSize(updateForm.getSize());
            doll.setType(updateForm.getType());
            doll.setImgUrl(updateForm.getImgUrl());
            doll.setDollNo(newDollNo);
            Db.updateById(doll);

            //更新订单
            Db.lambdaUpdate(OrderEntity.class)
                    .eq(OrderEntity::getDollNo, dollNo)
                    .set(OrderEntity::getDollNo, newDollNo)
                    .update();

            //更新用户别名表
            Db.lambdaUpdate(DollNickEntity.class)
                    .eq(DollNickEntity::getDollNo, dollNo)
                   .set(DollNickEntity::getDollNo, newDollNo)
                   .update();


        }

    }



    private String getDollNo(String officialName,String type,String size,String skin) {

        return officialName + "/" + type +"/"+ size +"/"+skin;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(DollDeleteForm req) {
        DollEntity doll = Db.getById(req.getId(), DollEntity.class);
        if(null == doll){
            throw new BusinessException("娃娃不存在");
        }

        Db.removeById(req.getId(),DollEntity.class);

        //清空订单关联信息
        Db.lambdaUpdate(OrderEntity.class)
                .eq(OrderEntity::getDollNo,doll.getDollNo())
                .set(OrderEntity::getDollNo,"")
                .update();


        //删除用户别名表
        Db.lambdaUpdate(DollNickEntity.class)
              .eq(DollNickEntity::getDollNo,doll.getDollNo())
              .remove();


    }

    public DollVO detail(DollDeleteForm req) {
        DollEntity doll = Db.getById(req.getId(), DollEntity.class);
        if(null == doll){
            throw new BusinessException("娃娃不存在");
        }
        DollVO bean = BeanUtil.toBean(doll, DollVO.class);
        return bean;
    }
}
